<template>
  <div class="classify app_content">
    <!-- 导航栏  titelText="新增编辑"-->
    <div class="header_body">
      <header-nav :slefBack="true" :titelText="titelText" @goBack="goBack"> </header-nav>
    </div>
    <div class="detail_body">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
<!--          <van-field-->
<!--              required-->
<!--              v-model="itemInfo.provinceCity"-->
<!--              is-link-->
<!--              readonly-->
<!--              name="area"-->
<!--              label="地区选择"-->
<!--              placeholder="点击选择省市"-->
<!--              @click="showArea = true"-->
<!--          />-->
<!--          <van-popup v-model:show="showArea" destroy-on-close position="bottom">-->
<!--            <cascader-->
<!--                v-model="cascaderValue"-->
<!--                title="请选择"-->
<!--                @close="showArea = false;"-->
<!--                @finish="onFinish"/>-->
<!--          </van-popup>-->
          <van-field
              required
              v-model="itemInfo.purchaserName"
              name="购机者名称"
              label="购机者名称"
              placeholder="购机者名称"
              :rules="[{ required: true, message: '请填写购机者名称' }]"
          />
          <van-field
              required
              v-model="itemInfo.equItem"
              name="机具品目"
              label="机具品目"
              placeholder="机具品目"
              :rules="[{ required: true, message: '请填写机具品目' }]"
          />
          <van-field
              required
              v-model="itemInfo.manufacturer"
              name="生产厂家"
              label="生产厂家"
              placeholder="生产厂家"
              :rules="[{ required: true, message: '请填写生产厂家' }]"
          />
          <van-field
              required
              v-model="itemInfo.productName"
              name="产品名称"
              label="产品名称"
              placeholder="产品名称"
              :rules="[{ required: true, message: '请填写产品名称' }]"
          />
          <van-field
              required
              v-model="itemInfo.purchaseModel"
              name="购买机型"
              label="购买机型"
              placeholder="购买机型"
              :rules="[{ required: true, message: '请填写购买机型' }]"
          />
          <van-field
              required
              v-model="itemInfo.purchaseQuantity"
              name="购买数量"
              label="购买数量"
              placeholder="购买数量"
              :rules="[{ required: true, message: '请填写购买数量' }]"
          />
          <van-field
              required
              v-model="itemInfo.dealer"
              name="经销商"
              label="经销商"
              placeholder="经销商"
              :rules="[{ required: true, message: '请填写经销商' }]"
          />
          <van-field
              required
              v-model="itemInfo.purchaseDate"
              is-link
              readonly
              name="datePicker"
              label="购机日期"
              placeholder="点击选择日期"
              @click="showPickerData = true"
          />
          <van-popup v-model:show="showPickerData" destroy-on-close position="bottom">
            <van-date-picker
                :model-value="pickerValueData"
                @confirm="onConfirmData"
                @cancel="showPickerData = false"
            />
          </van-popup>
          <van-field
              required
              v-model="itemInfo.salesPrice"
              name="单台销售价格"
              label="单台销售价格"
              placeholder="单台销售价格"
              :rules="[{ required: true, message: '请填写单台销售价格' }]"
          />
          <van-field
              required
              v-model="itemInfo.centralSubsidyAmount"
              name="单台中央补贴额"
              label="单台中央补贴额"
              placeholder="单台中央补贴额"
              :rules="[{ required: true, message: '请填写单台中央补贴额' }]"
          />
          <van-field
              required
              v-model="itemInfo.totalSubsidyAmount"
              name="总补贴额"
              label="总补贴额"
              placeholder="总补贴额"
              :rules="[{ required: true, message: '请填写总补贴额' }]"
          />
          <van-field
              required
              v-model="itemInfo.factoryNumber"
              name="出厂编号"
              label="出厂编号"
              placeholder="出厂编号"
              :rules="[{ required: true, message: '请填写出厂编号' }]"
          />
<!--          <van-field required name="radio" label="状态">-->
<!--            <template #input>-->
<!--              <van-radio-group v-model="itemInfo.checkType" direction="horizontal">-->
<!--                <van-radio name="1">年审</van-radio>-->
<!--                <van-radio name="2">初审</van-radio>-->
<!--              </van-radio-group>-->
<!--            </template>-->
<!--          </van-field>-->
          <van-field required name="radio" label="是否超录申请">
            <template #input>
              <van-radio-group v-model="itemInfo.checkState">
                <van-radio style="margin-bottom: 5px" name="是">是</van-radio>
                <van-radio style="margin-bottom: 5px" name="否">否</van-radio>
              </van-radio-group>
            </template>
          </van-field>
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>

    </div>
  </div>
</template>
<script setup name="ManagementScanItemEdit">
import Cascader from "@/components/cascader/index.vue"
import {showDialog} from "vant";
import {ADD_SAVE_SQBL, UPDATE_SAVE_SQBL} from "@/api/subsidyApplication.js";
const router = useRouter()
const titelText = ref("")
const itemInfo = ref({
  appointmentId: ""
})
onMounted(() => {
  const query = router?.currentRoute?.value?.query;
  itemInfo.value = query.itemInfo ? JSON.parse(query.itemInfo) : {}
  titelText.value = itemInfo.value.appointmentId ? '编辑预约信息' : '新增预约信息'
  if (itemInfo.value.cityName) {
    itemInfo.value.provinceCity = itemInfo.value.cityName + itemInfo.value.districtName
  } else {
    itemInfo.value.provinceCity = ""
  }
})
const result = ref('');
const showArea = ref(false);

const showPickerData = ref(false);
const pickerValueData = ref([]);
const onConfirmData = ({ selectedValues }) => {
  itemInfo.value .purchaseDate= selectedValues.join('-');
  pickerValueData.value = selectedValues;
  showPickerData.value = false;
};
const cascaderValue = ref('');
const onFinish = ({ selectedOptions }) => {
  showArea.value = false
  cascaderValue.value = selectedOptions.map((option) => option.name).join('/');
  itemInfo.value.cityCode = selectedOptions[0].code
  itemInfo.value.cityName = selectedOptions[0].name
  itemInfo.value.districtCode = selectedOptions[1].code
  itemInfo.value.districtName = selectedOptions[1].name
  itemInfo.value.provinceCity = itemInfo.value.cityName + itemInfo.value.districtName
};
const onSubmit = async (values) => {
  const id = itemInfo.value.appointmentId;
  let res
  if (id) {
    res = await UPDATE_SAVE_SQBL(itemInfo.value);
  } else {
    res = await ADD_SAVE_SQBL(itemInfo.value);
  }
  if (res?.code == 20000) {
    showDialog({
      message: '操作成功',
    }).then(() => {
      goBack()
    });
  }
};
const goBack = () => {
  router.go(-1);
};
</script>
<style lang="less" scoped>
.classify {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}
</style>